﻿

#region Usings

using PolyGameEngine.Tools;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

#endregion
namespace PolyGameEngine.MenuSystem
{
    public class MainMenu
    {
        GameState gameState
        {
            get { return PhysicsGame.gamestate; }
            set { PhysicsGame.ChangeGameState(value); }
        }
        public delegate void PlayGameSelected();
        public event PlayGameSelected PlayGameSelectedEvent;

        public delegate void SelectLevelSelected();
        public event SelectLevelSelected SelectLevelSelectedEvent;

        public delegate void AboutSelected();
        public event AboutSelected AboutSelectedEvent;

        public delegate void EditorSelected();
        public event EditorSelected EditorSelectedEvent;

        public delegate void HelpSelected();
        public event HelpSelected HelpSelectedEvent;

        public delegate void ExitSelected();
        public event ExitSelected ExitSelectedEvent;
        Texture2D radial;
        Texture2D shadow;
        Texture2D filter;
        Texture2D oshadow;
        Texture2D poly;
        Texture2D morph;
        Texture2D[] backGround = new Texture2D[6];



        int currentItem = 0;

        string[] itemNames = { "Play", "Level Select", "About", "Level editor", "Help", "Exit" };
        float _degrees = 0;

        float degrees
        {
            get
            {
                return MathHelper.ToRadians(_degrees);
            }
        }


        ContentManager content;
        Viewport viewport;
        SpriteFont[] fonts;


        public MainMenu(ContentManager Content, Viewport Viewport, SpriteFont[] Fonts)
        {
            fonts = Fonts;
            content = Content;
            viewport = Viewport;
            shadow = Content.Load<Texture2D>("Menu/Shadow");
            oshadow = Content.Load<Texture2D>("Menu/OuterShadow");
            radial = Content.Load<Texture2D>("Menu/MenuWheel");
            filter = Content.Load<Texture2D>("Menu/filter");
            poly = Content.Load<Texture2D>("Menu/Poly");
            morph = Content.Load<Texture2D>("Menu/Morph");
            backGround[0] = Content.Load<Texture2D>("Menu/steel");
            backGround[1] = Content.Load<Texture2D>("Menu/water");
            backGround[2] = Content.Load<Texture2D>("Menu/plastic");
            backGround[3] = Content.Load<Texture2D>("Menu/rubber");
            backGround[4] = Content.Load<Texture2D>("Menu/fire");
            backGround[5] = Content.Load<Texture2D>("Menu/wood");
        }
       


        float goaldegree;
        float lettergrow = 0.3f;
        float Logogrow = 1f;
        float sgrow = 1f;
        bool letgrow = true;
        bool loggrow = true;
        bool shadowgrow = true;


        bool clockwise = true;

        public void update()
        {
            InputMan.Update();

            if (InputMan.OnPress(Keys.Enter))
                AudioMan.PlayCue("Menu Select");

            if (InputMan.OnPress(Keys.Enter) && currentItem == 5)
            {
                if (ExitSelectedEvent != null)
                    ExitSelectedEvent();
                gameState = GameState.Close;
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 0)
            {
                gameState = GameState.Play;
                if (PlayGameSelectedEvent != null)
                {
                    PlayGameSelectedEvent();
                    if (AudioMan.getPause())
                        AudioMan.PauseMusicOff();
                    AudioMan.menuStop();
                    AudioMan.PlayMusic();
                }
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 1)
            {
                gameState = GameState.LevelSelect;
                if (SelectLevelSelectedEvent != null)
                    SelectLevelSelectedEvent();
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 2)
            {
                gameState = GameState.About;
                if (AboutSelectedEvent != null)
                    AboutSelectedEvent();
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 3)
            {
                gameState = GameState.Edit;
                if (EditorSelectedEvent != null)
                    EditorSelectedEvent();
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 4)
            {
                gameState = GameState.Help;
                if (HelpSelectedEvent != null)
                    HelpSelectedEvent();
            }


            if (InputMan.OnPress(Keys.S))
            {
                lettergrow = 1.5f;
                if (++currentItem > 5)
                {
                    currentItem = 0;
                }
            }

            if (InputMan.OnPress(Keys.W))
            {
                lettergrow = 1.5f;
                if (--currentItem < 0)
                {
                    currentItem = 5;
                }
            }

            if (InputMan.OnPress(Keys.S))
            {
                clockwise = true;
                goaldegree = currentItem * 60;
                AudioMan.PlayCue("Menu Switch");
            }
            if (InputMan.OnPress(Keys.W))
            {
                clockwise = false;
                goaldegree = currentItem * 60;
                AudioMan.PlayCue("Menu Switch");
            }



            if (clockwise)
            {
                if (_degrees < goaldegree || (_degrees > goaldegree && degrees < 360))
                    _degrees += 3;
                if (_degrees > 360)
                    _degrees = 0;
            }
            else
            {
                if (_degrees > goaldegree || (_degrees < goaldegree))
                    _degrees -= 3;
                if (_degrees < 0)
                {
                    _degrees = 360;
                }
            }

            if (lettergrow >= 1f)
                letgrow = false;
            if (lettergrow <= 0.8f)
                letgrow = true;
            if (letgrow)
                lettergrow += 0.005F;
            else
                lettergrow -= 0.005f;

            if (Logogrow >= 1f)
                loggrow = false;
            if (Logogrow <= 0.97f)
                loggrow = true;
            if (loggrow)
                Logogrow += 0.0005F;
            else
                Logogrow -= 0.0005f;

            if (sgrow >= 1f)
                shadowgrow = false;
            if (sgrow <= 0.97f)
                shadowgrow = true;
            if (shadowgrow)
                sgrow += 0.0005F;
            else
                sgrow -= 0.0005f;


        }

        public void Draw(SpriteBatch sb)
        {
            #region background
            float shScale = sgrow;
            float filterScale = shScale;
            float radScale = filterScale * 0.75f;
            float offset = 0.7f;
            Vector2 centerScreen = new Vector2(viewport.Width / 2, viewport.Height / 2);
            Vector2 sorigin = new Vector2((shadow.Width / 2), shadow.Height / 2);
            Vector2 rorigin = new Vector2((radial.Width / 2), radial.Height / 2);
            Vector2 forigin = new Vector2((filter.Width / 2), filter.Height / 2);
            Vector2 radpos = new Vector2(viewport.Width * offset, centerScreen.Y);
            Vector2 Vpoly = new Vector2((int)(viewport.Width * 0.1), (int)(viewport.Height * 0.1));



            sb.Draw(backGround[currentItem], centerScreen, new Rectangle(0, 0, backGround[currentItem].Width, backGround[currentItem].Height), Color.White, 0, new Vector2(backGround[currentItem].Width / 2, backGround[currentItem].Height / 2), 1, SpriteEffects.None, 0.5f);
            sb.Draw(oshadow, viewport.Bounds, new Rectangle(0, 0, oshadow.Width, oshadow.Height), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.5f);
            sb.Draw(shadow, radpos, new Rectangle(0, 0, shadow.Width, shadow.Height), Color.White, 0, sorigin, shScale, SpriteEffects.None, 0.5f);
            sb.Draw(radial, radpos, new Rectangle(0, 0, radial.Width, radial.Height), Color.White, degrees, rorigin, radScale, SpriteEffects.None, 0.5f);
            sb.Draw(filter, radpos, new Rectangle(0, 0, filter.Width, filter.Height), Color.White, 0, forigin, filterScale, SpriteEffects.None, 0.5f);
            sb.Draw(poly, Vpoly, new Rectangle(0, 0, poly.Width, poly.Height), Color.White, 0, Vector2.Zero, Logogrow, SpriteEffects.None, 0.5f);
            sb.Draw(morph, Vpoly + new Vector2(poly.Width - 20, 0), new Rectangle(0, 0, morph.Width, morph.Height), Color.White, 0, Vector2.Zero, Logogrow, SpriteEffects.None, 0.5f);
            #endregion



            Vector2 k;
            k = new Vector2((float)(viewport.Width * 0.1), (float)(viewport.Height * 0.3));

            int j = currentItem - 1;
            if (j < 0)
                j = fonts.Length - 1;

            for (int i = 0; i < itemNames.Length; i++)
            {

                if (currentItem == i)

                    sb.DrawString(fonts[0], itemNames[i], k, Color.Red, 0, Vector2.Zero, lettergrow, SpriteEffects.None, 0);
                else

                    sb.DrawString(fonts[0], itemNames[i], k, Color.Black);

                k.Y += 0.1f * viewport.Height;

            }


        }
    }
}
